# coding=utf-8
# author:xc
# datetime:2018/8/3 10:08
from . import admin
from dbConnection.MysqlConn import Mysql
from app import util
from flask import render_template,request


@admin.route('/admin/model/list.html', methods=['POST', 'GET'])
@util.admin_login
def admin_wash_model():
    mysql = Mysql()
    sql = "select * from wash_model where status=1"
    result = mysql.getAll(sql, None)
    mysql.dispose()
    code = util.CODE_EMPTY
    data = []
    if result:
        code = util.CODE_SUCCESS
        data = result

    data = {
        'data': data,
        'code': code
    }
    return render_template('admin/store/model_list.html', res=data)


@admin.route('/admin/model/newly.html', methods=['POST', 'GET'])
@util.admin_login
def admin_model_newly():
    if request.method == "GET":
        return render_template('admin/store/model_newly.html')
    else:
        try:
            modelName = request.values.get('modelName')
            price = request.values.get('price')
            mysql = Mysql()
            sql = "insert into wash_model(modelName,modelPrice,addTime)VALUES ('%s','%s',now())" % (
                modelName, price)
            result = mysql.update(sql, None)
            code = util.CODE_EMPTY
            mysql.dispose()
            if result > 0:
                code = util.CODE_SUCCESS
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)


@admin.route('/admin/model/edit.html', methods=['POST', 'GET'])
@util.admin_login
def admin_model_edit():
    if request.method == 'GET':
        modelId = request.args.get('modelId')
        mysql = Mysql()
        sql = "select * from wash_model where modelId='%s'" % modelId
        result = mysql.getOne(sql, None)
        mysql.dispose()
        return render_template("admin/store/model_edit.html", res=result)
    else:
        try:
            modelName= request.values.get('modelName')
            price= request.values.get('modelPrice')
            modelId = request.values.get('modelId')
            mysql = Mysql()
            sql = "update wash_model set modelName='%s',modelPrice='%s' where modelId='%s'" % (
                modelName, price, modelId)
            result = mysql.update(sql, None)
            code = util.CODE_EMPTY
            mysql.dispose()
            if result > 0:
                code = util.CODE_SUCCESS
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)


@admin.route('/admin/model/delete', methods=['POST'])
@util.admin_login
def admin_model_delete():
    modelId = request.values.get('modelId')
    mysql = Mysql()
    sql = "delete from wash_model where modelId='%s' " % modelId
    res = mysql.delete(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)